home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / YANNS3D.ZIP / STUFF / OLDSQR.TXT < prev   
Encoding:
Text File  |  1993-09-26  |  2.0 KB  |  105 lines

  1. ; ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2. ;  Function to calc 16 bit square root of 32 bit number in DI·SI, ret in AX
  3. ; ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  4. .DATA
  5. SRRoots    DB 0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,225
  6.     EVEN
  7. SRUnp    DW ? ; Number of bytes unprocessed
  8.  
  9. .CODE
  10.     EVEN
  11. SqRoot    PROC
  12.     mov    ax,ds
  13.     mov    es,ax
  14.     mov    dx,di
  15.     mov    cx,si
  16.     xor    ah,ah
  17.     or    dh,dh        ; Is most sig. byte nonzero?
  18.     jz    SRL1
  19.     mov    al,dh
  20.     mov    dh,dl
  21.     mov    dl,ch
  22.     mov    ch,cl
  23.     mov    [SRUnp],3    ; 3 bytes unprocessed
  24.     jmp    SRL6
  25. SRL1:    or    dl,dl
  26.     jz    SRL2
  27.     mov    al,dl
  28.     mov    dh,ch
  29.     mov    dl,cl
  30.     mov    [SRUnp],2    ; 2 bytes unprocessed
  31.     jmp    SRL6
  32. SRL2:    or    ch,ch
  33.     jz    SRL3
  34.     mov    al,ch
  35.     mov    dh,cl
  36.     mov    [SRUnp],1    ; 1 byte unprocessed
  37.     jmp    SRL6
  38. SRL3:    or    cl,cl
  39.     jz    SRL5
  40.     mov    al,cl
  41.     mov    [SRUnp],0    ; 0 bytes unprocessed
  42.     jmp    SRL6
  43. SRL5:    xor    ax,ax
  44.     ret
  45. SRL6:    mov    di,OFFSET SRRoots
  46.     mov    si,10h
  47. SRL7:    scasb
  48.     je    SRL9
  49.     jb    SRL8
  50.     dec    si
  51.     jnz    SRL7
  52.     inc    di
  53. SRL8:    dec    di
  54.     inc    si
  55. SRL9:    mov    bl,[di-1]
  56.     xor    bh,bh        ; BX: max square below AL
  57.     dec    si
  58.     xor    si,0Fh        ; SI in (0,15) (index into sqr table)
  59.     mov    di,si
  60.     mov    bp,[SRUnp]    ; Test if we have already finished
  61.     or    bp,bp        ; ^
  62.     jz    SRExit        ; ^
  63.     sub    ax,bx        ; Substract max square (already taken into
  64.                 ;  account in DI)
  65.     ; Loop for unprocessed bytes
  66. SRMainLoop:
  67.     mov    ah,al        ; Shift into AX next byte
  68.     mov    al,dh        ; ^
  69.     mov    dh,dl        ; ^
  70.     mov    dl,ch        ; ^
  71.     mov    ch,cl        ; ^
  72.     mov    si,di
  73.     REPT 5
  74.     shl    si,1
  75.     ENDM
  76.     mov    bp,dx        ; Save for later restore
  77.     xor    dx,dx
  78.     mov    cx,ax        ; Save for later restore (can use CX
  79.                 ;  because we have already processed
  80.                 ;  2 bytes, so there can only 2 left
  81.                 ;  in DX)
  82.     div    si
  83.     REPT 4
  84.     rol    di,1
  85.     ENDM
  86.     add    di,ax
  87.     add    si,ax
  88. SRL10:    imul    si
  89.     mov    dx,ax
  90.     mov    ax,cx        ; Restore previous (before DIV) value
  91.     sub    ax,dx
  92.     jc    SRL12
  93.     mov    dx,bp        ; Restore previous DX value (unp. bytes)
  94.     dec    [SRUnp]
  95.     jnz    SRMainLoop
  96. SRExit: mov    ax,di
  97.     ret
  98.  
  99. SRL12:    dec    si
  100.     dec    di
  101.     mov    ax,si
  102.     and    ax,001Fh
  103.     jmp    SRL10
  104.  
  105. SqRoot    ENDP